package com.example.demo.controller;
/* @Author      zouzh
 * @PackageName BlogSystem-sm
 * @Package     com.example.demo.controller
 * @Date        2023/9/5 13:52
 */

import com.example.demo.common.AppVar;
import com.example.demo.common.PasswordUtils;
import com.example.demo.common.ResultAjax;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册接口
     * @param userInfo
     * @return
     */
    @RequestMapping("/reg")
    public ResultAjax reg(UserInfo userInfo){
        // 1.校验参数
        if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword())){
            //参数异常
            return ResultAjax.fail(-1, "非法参数");
        }
        // 密码加盐
        userInfo.setPassword(PasswordUtils.encrypt(userInfo.getPassword()));
        // 2.请求 service 进行添加操作
        int result = userService.reg(userInfo);
        // 3.将执行的结果返回给前端
        return ResultAjax.succ(result);
    }

    /**
     * 登录接口
     * @param userInfo
     * @param request
     * @return
     */
   @RequestMapping("/login")
    public ResultAjax login(UserInfo userInfo, HttpServletRequest request){
        // 1. 参数校验
       if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername())
               || !StringUtils.hasLength(userInfo.getPassword())){
           System.out.println("Userinfo = " + userInfo.toString());
           return ResultAjax.fail(-1, "非法参数");
       }
        // 2. 根据用户名查询对象
        UserInfo user = userService.getUserByName(userInfo.getUsername());
       if (user == null || user.getId() <= 0){
           return ResultAjax.fail(-2, "用户名或密码错误");
       }
        // 3. 使用对象中的密码和用户输入的密码进行比较
       // !user.getPassword().equals(userInfo.getPassword())
       if (! PasswordUtils.decrypt(userInfo.getPassword(),user.getPassword())){
           return ResultAjax.fail(-2, "用户名或密码错误");
       }
        // 4. 比较成功之后，将对象存储到 session 中
       HttpSession session = request.getSession(true);
       session.setAttribute(AppVar.SESSION_USERINFO_KEY, user);
        // 5. 将结果返回给用户
       return ResultAjax.succ(1);

   }

    /**
     * 注销接口
     */

    @RequestMapping("/logout")
    public ResultAjax logout(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(AppVar.SESSION_USERINFO_KEY) != null){
            session.removeAttribute(AppVar.SESSION_USERINFO_KEY);
        }
        return ResultAjax.succ(1);
    }
}
